package com.css.dzj.zfjg.modules.admforce.service.Impl;

import com.css.dzj.zfjg.common.util.*;
import com.css.dzj.zfjg.modules.admforce.entity.JgJgxwForceActionEntity;
import com.css.dzj.zfjg.modules.admforce.entity.QJgJgxwForceActionEntity;
import com.css.dzj.zfjg.modules.admforce.mapper.ForceMapper;
import com.css.dzj.zfjg.modules.admforce.service.JgJgxwForceActionService;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.entity.QJgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.taskinfo.vo.rwjgLh;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 行政强制行为信息实现类
 * @author wt
 * @date 2020\2\20 0020
 */
@Service
public class JgJgxwForceActionServiceImpl implements JgJgxwForceActionService {

    @Autowired
    private ForceMapper fMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;

    @Autowired
    HttpServletRequest request;


    /**
     * 添加行行政强制行为信息
     */
    @Override
    @Transactional
    public void add(JgJgxwForceActionEntity forceActionEntity) {

        try{
            forceActionEntity.setId(SnowflakeId.getId());
            forceActionEntity.setCdTime(new Date());
            forceActionEntity.setCreateTime(new Date());
            //记录唯一标识
            forceActionEntity.setRecordUniqueIdentity(SnowflakeId.getId());
            //新增数据类型i-insert,u-update,d-delete
            forceActionEntity.setCdOperation("i");
            //来源
            forceActionEntity.setCdSource("07");
            //状态0是未删除，1是已删除
            forceActionEntity.setFlag(Constant.NO_DELETE);
            //创建人
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")){
                LoginUser userInfo =  (LoginUser)r.get("data");
                forceActionEntity.setCreateUser(userInfo.getLoginName());
            }
            fMapper.save(forceActionEntity);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 修改行政强制行为信息
     */
    @Override
    @Transactional
    public void update(JgJgxwForceActionEntity forceActionEntity) {

    }

    /**
     * 通过id查询行政强制行为信息
     */
    @Override
    public JgJgxwForceActionEntity findById(String id) {
        return fMapper.findById(id).get();
    }

    /**
     * 分页查询行政强制行为信息
     */
    @Override
    public QueryResults<JgJgxwForceActionEntity> queryPage(Pageable pageable) {
        QJgJgxwForceActionEntity qForce = QJgJgxwForceActionEntity.jgJgxwForceActionEntity;
        QueryResults<JgJgxwForceActionEntity> forceResults = jpaQueryFactory.selectFrom(qForce)
                .where(qForce.flag.eq(Constant.NO_DELETE))
                .orderBy(qForce.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return forceResults;
    }

    /**
     * 删除行政强制行为信息
     */
    @Override
    @Transactional
    public Boolean delete(String id) {
        QJgJgxwForceActionEntity qForce = QJgJgxwForceActionEntity.jgJgxwForceActionEntity;
        if(StringUtils.isEmpty(id)){
            return  false;
        }
        try{
            jpaQueryFactory
                    .update(qForce)
                    .set(qForce.flag,Constant.DELETE)
                    .where(
                            qForce.flag.eq(Constant.NO_DELETE),
                            qForce.id.eq(id)
                    )
                    .execute();
            return true;
        }catch (Exception e){
            return false;
        }
    }
    /**
     *根据编码查询行为信息
     */
    @Override
    public rwjgLh findOneById(String actionId) {
        if(StringUtils.isEmpty(actionId)) {
            return null;
        }
        QJgJgxwForceActionEntity q = QJgJgxwForceActionEntity.jgJgxwForceActionEntity;
        JgJgxwForceActionEntity fetchOne = jpaQueryFactory
                .selectFrom(q)
                .where(q.id.eq(actionId),
                        q.cdOperation.eq(CvaConstants.UN_DELETED_FLAG)
                )
                .fetchOne();
        rwjgLh vo = new rwjgLh();

        if(fetchOne != null &&  !StringUtils.isEmpty(fetchOne.getSuperviseObject())) {
            QJgLhjgJcdxEntity jcdx = QJgLhjgJcdxEntity.jgLhjgJcdxEntity;
            JgLhjgJcdxEntity dx = jpaQueryFactory
                    .selectFrom(jcdx)
                    .where(jcdx.enterprisecreditcode.eq(fetchOne.getSuperviseObject())
                    )
                    .fetchFirst();
            vo.setJgLhjgJcdxEntity(dx);
        }

        vo.setJgJgxwForceActionEntity(fetchOne);

        return vo;
    }

    /**
     * 物理删除强制行为信息
     * @param forceActionEntity
     */
    @Override
    public void physicalDelete(JgJgxwForceActionEntity forceActionEntity) {
        try {
            fMapper.delete(forceActionEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
